/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.impl;

import com.qdu.dao.CollectDao;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Random; 

/**
 *
 * @author SBH
 */
public class CollectImpl implements CollectDao {

    private int SUCCESS = 1;
    private int ERROR = 2;
    private int SAME = 3;

    @Override
    public int addShouCang(int collect_bookid, String collect_userid) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        Random random = new Random();

        int r = random.nextInt(100);

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select collect_amount from collect where collect_bookid=? and collect_userid=?");
            ps.setInt(1, collect_bookid);
            ps.setString(2, collect_userid);
            rs = ps.executeQuery();
            con.commit();
            if (rs.next()) {
                int j = rs.getInt(1);
                j += 1;
                // 修改收藏夹
                ps = con.prepareStatement("update collect set collect_amount=? where collect_bookid=?");
                ps.setInt(1, j);
                ps.setInt(2, collect_bookid);
                int x = ps.executeUpdate();
                if (x == 1) {
                    return this.SUCCESS;
                } else {
                    return this.ERROR;
                }
            } else {
                // 向2收藏夹表中添加信息
                ps = con.prepareStatement("insert into collect(collect_id,collect_bookid,collect_amount,collect_addtime,collect_userid) values(?,?,?,?,?)");
                ps.setInt(1, r);
                ps.setInt(2, collect_bookid);
                ps.setInt(3, 1);
                ps.setString(4, new java.util.Date().toLocaleString());
                ps.setString(5, collect_userid);
                int i = ps.executeUpdate();
                if (i == 1) {
                    return this.SUCCESS;
                } else {
                    return this.ERROR;
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }

    }

    @Override
    public int delShouCang(int collect_bookid) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("delete from collect where collect_bookid=? ");
            ps.setInt(1, collect_bookid);
            con.commit();

            int v = ps.executeUpdate();
            if (v == 1) {
                return this.SUCCESS;
            } else {
                return this.ERROR;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
    }

    @Override
    public List getMyCollect(String collect_userid) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List list = new ArrayList();
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select bookinformation.book_name,collect.collect_amount,bookinformation.book_price,collect.collect_addtime,collect.collect_bookid from bookinformation,collect where collect.collect_bookid=bookinformation.book_id and collect.collect_userid=? order by collect.collect_id desc ");
            ps.setString(1, collect_userid);
            rs = ps.executeQuery();
            con.commit();
            while (rs.next()) {
                List list2 = new ArrayList();
                list2.add(rs.getString(1));
                list2.add(rs.getString(2));
                list2.add(rs.getString(3));
                list2.add(rs.getString(4));
                list2.add(rs.getString(5));
                list.add(list2);

            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return list;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }

    }

}
